package com.xms.utils;

import cn.hutool.core.lang.Assert;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * <p>
 * 字符串工具类
 * </p>
 *
 * @author migo
 * @since 2021/8/31
 */
public class StringUtil {
    private static String[] SpeciaCharaters = {".", "*", "?", "\\", "|", "\"", "'"};

    public static boolean notEmpty(String s){
        return s != null && !s.equals("");
    }

    public static boolean notEmpty(Object s){
        return s != null && !s.toString().isEmpty();
    }

    public static boolean isEmpty(String s){
        return s == null || s.isEmpty();
    }
    public static boolean isEmpty(Object s){
        return s == null || s.toString().isEmpty();
    }

    public static boolean startsWithIgnoreCase(String s, String prefix){
        if(isEmpty(s) || isEmpty(prefix)){
            return false;
        }
        return s.toLowerCase(Locale.ROOT).startsWith(prefix.toLowerCase(Locale.ROOT));
    }
    public static boolean endsWithIgnoreCase(String s, String suffix){
        if(isEmpty(s) || isEmpty(suffix)){
            return false;
        }
        return s.toLowerCase(Locale.ROOT).endsWith(suffix.toLowerCase(Locale.ROOT));
    }

    public static boolean containsIgnoreCase(String s, String contains){
        if(isEmpty(s) || isEmpty(contains)){
            return false;
        }
        return s.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
    }

    public static boolean equalSafe(String s1, String s2){
        if(isEmpty(s1) || isEmpty(s2)){
            return false;
        }
        return s1.equalsIgnoreCase(s2);
    }

    /**
     * 数组转换成字符，默认以逗号分隔
     * @param items
     * @return
     */
    public static String join(Object[] items){
        return StringUtil.join(Arrays.asList(items), ",", "");
    }

    public static String join(Object[] items, String separator){
        return StringUtil.join(Arrays.asList(items), separator, "");
    }

    public static String join(Collection<Object> items, String separator){
        return StringUtil.join(items, separator, "");
    }

    public static String join(Collection<Object> items, String separator, String wrapItem){
        String result = "";
        if (items != null && !items.isEmpty())
        {
            List<String> strItems = new LinkedList<>();
            for (Object item : items)
            {
                strItems.add(item.toString());
            }
            if (!wrapItem.isEmpty())
            {
                List<String> newList = new LinkedList<>();
                for (Object item : strItems)
                {
                    if (item != null)
                    {
                        newList.add(wrapItem + item.toString() + wrapItem);
                    }
                }
                result = String.join(separator, newList);
            }
            else
            {
                result = String.join(separator, strItems);
            }
        }
        return result;
    }

    public static String safeCharacters(String value){
        if (value == null || value.isEmpty())
        {
            return value;
        }
        String safeValue = new String();
        byte[] b = value.getBytes(StandardCharsets.UTF_8);
        for (int i = 0;i < b.length; i++){
            int finalI = i;
            if(Arrays.stream(SpeciaCharaters).anyMatch(x->x.equalsIgnoreCase(String.valueOf(b[finalI])))){
                safeValue += ("\\" + b.toString()).getBytes(StandardCharsets.UTF_8);
            }
            else{
                safeValue += String.valueOf(b[finalI]);
            }
        }
        return safeValue;
    }

    public static String[] splitSafe(String value, String separator)
    {
        if (value == null || value.isEmpty())
        {
            return new String[0];
        }

        String safeValue = value;//safeCharacters(value);
        return safeValue.split(separator);
    }

    public static String urlDecode(String value) {
        if(value != null && !value.isEmpty())
            try {
                return URLDecoder.decode(value, "UTF-8");
            }catch(UnsupportedEncodingException e){
                return value;
            }
        return value;
    }

    public static String urlEncode(String value) {
        if(value != null && !value.isEmpty())
            try {
                return URLEncoder.encode(value, "UTF-8");
            }catch(UnsupportedEncodingException e){
                return value;
            }
        return value;
    }

    /**
     * 压缩字符串，去掉 前后空格 \t制表符 \n换行 \r回车
     * @param value
     * @return
     */
    public static String compress(String value){
        if(isEmpty(value))
            return value;
        return value.trim().replaceAll("[\\t\\n\\r]", "");
    }
}
